# coding:utf-8
import tensorflow as tf
import cv2
import os
import numpy as np
path=r"\\10.244.134.184\dba_train\BIgdata專案\彩盒智能判定系統\3.模塊及集成開發\5.AI\model\140direction_object_model\20181204\ws2_keras_mobile_model_1204_no_tl.pb"
PATH_TO_TEST_IMAGES_DIR = r'F:\bigphoto\test_images'
TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, i) for i in os.listdir(PATH_TO_TEST_IMAGES_DIR)]
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef() # 获取序列化后的图
    # 载入pb文件
    with tf.gfile.GFile(path,'rb') as fid:
        serialized_graph=fid.read()
        # 解析内容
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def,name='')
# REW:打印节点
print(detection_graph.get_operations())
input_1 = detection_graph.get_tensor_by_name('input_1:0')  # 获取名字
oupu = detection_graph.get_tensor_by_name('output_1:0')
with tf.Session(graph=detection_graph) as sess:
    for image_path in TEST_IMAGE_PATHS:
        gray = cv2.imread(image_path)
        gray = cv2.cvtColor(gray, cv2.COLOR_RGB2GRAY)
        gray = cv2.resize(gray, dsize=(180, 180))
        # cv2.imshow('')
        gray = np.expand_dims(gray, axis=2)  # 第三维度
        gray = np.expand_dims(gray,axis=0)

        # input_1 = detection_graph.get_tensor_by_name('conv2d_1_input:0')
        # t = detection_graph.get_tensor_by_name('dropout_1/keras_learning_phase:0')

        # prediction = sess.run([oupu], feed_dict={input_1: gray})
        prediction = sess.run([oupu], feed_dict={input_1: gray})
        print(image_path)
        print('score,',prediction)
        print(np.argmax(prediction,axis=1),'\n\n')
        # pred = np.argmax(result, axis=1)
        # print(result[0][pred])
        # break

